from Autotests.BaseTest import AirClientTestCase
from Autotests.LocalMachine.Pages.AirClient.OptionsPage.ScheduleTab import ScheduleTab, DayOfWeek
from Autotests.LocalMachine.Pages.AirClient.OptionsPage.ScriptTab import ScriptsTab
from Autotests.LocalMachine.Pages.AirClient.BackupPage import BackupPlugins


class OptionsAddSchedule(AirClientTestCase):
    def runTest(self):
        self.scheduleName = 'Test'
        self.fireTime = '12:00'
        self.preBackupScript = '1'
        self.dayOfWeek = DayOfWeek.monday
        self.plugin = BackupPlugins.vssExchange
        self.postBackupScript = '2'
        self.scriptsTab = ScriptsTab()
        self.scheduleTab = ScheduleTab()

        self.scriptsTab.addScript(self.preBackupScript)
        self.scriptsTab.addScript(self.postBackupScript)
        countBefore = self.scheduleTab.getScheduleCount()
        self.scheduleTab.addSchedule(self.scheduleName, self.fireTime, self.preBackupScript, self.dayOfWeek,
                                     self.plugin, self.postBackupScript)
        countAfter = self.scheduleTab.getScheduleCount()
        self.assertEqual(countBefore + 1, countAfter,
                         "Schedule count hasn't been incremented after adding schedule")

    def tearDownImpl(self):
        self.scheduleTab.deleteSchedule(self.scheduleName)
        self.scriptsTab.deleteScript(self.preBackupScript)
        self.scriptsTab.deleteScript(self.postBackupScript)


class OptionsDeleteSchedule(AirClientTestCase):
    def runTest(self):
        scheduleToDelete = "ScheduleToDelete"
        scheduleTab = ScheduleTab()
        scheduleTab.addSchedule(scheduleToDelete)
        scheduleCount = scheduleTab.getScheduleCount()
        scheduleTab.deleteSchedule(scheduleToDelete)
        self.assertEqual(scheduleTab.getScheduleCount(), scheduleCount-1,
                         "Schedule count hasn't been decremented after deleting schedule")


class OptionsEditSchedule(AirClientTestCase):
    def runTest(self):
        self.scheduleToEdit = 'ScheduleToEdit'
        self.newScheduleName = 'NewSchedule'
        self.newFireTime = '15:30'
        self.newPreBackupScript = '2'
        self.newDayOfWeek = DayOfWeek.friday
        self.newPlugin = BackupPlugins.filesystem
        self.newPostBackupScript = '1'
        infoTuple = (self.newScheduleName, self.newFireTime, self.newPreBackupScript, self.newDayOfWeek,
                     self.newPlugin, self.newPostBackupScript)
        self.scriptsTab = ScriptsTab()
        self.scriptsTab.addScript(self.newPreBackupScript)
        self.scriptsTab.addScript(self.newPostBackupScript)
        self.scheduleTab = ScheduleTab()
        self.scheduleTab.deleteSchedule(self.scheduleToEdit)
        self.scheduleTab.deleteSchedule(self.newScheduleName)

        self.scheduleTab.addSchedule(self.scheduleToEdit)
        countBefore = self.scheduleTab.getScheduleCount()
        self.scheduleTab.editSchedule(self.scheduleToEdit, *infoTuple)
        scheduleInfo = self.scheduleTab.getScheduleInfo(self.newScheduleName)
        countAfter = self.scheduleTab.getScheduleCount()
        self.assertEqual(countBefore, countAfter, "Schedule count has been changed after modifying")
        self.assertEqual(infoTuple, scheduleInfo, "New schedule attributes hasn't been applied after modifying")

    def tearDownImpl(self):
        self.scheduleTab.deleteSchedule(self.newScheduleName)
        self.scriptsTab.deleteScript(self.newPreBackupScript)
        self.scriptsTab.deleteScript(self.newPostBackupScript)


class OptionsSetScheduleEnabled(AirClientTestCase):
    def runTest(self):
        self.scheduleName = 'Test'
        self.scheduleTab = ScheduleTab()
        self.scheduleTab.addSchedule(self.scheduleName)
        self.scheduleTab.setScheduleEnabled(self.scheduleName, False)
        scheduleState = self.scheduleTab.isScheduleEnabled(self.scheduleName)
        self.assertEqual(False, scheduleState, "Schedule hasn't been enabled")

    def tearDownImpl(self):
        self.scheduleTab.deleteSchedule(self.scheduleName)


import unittest
if __name__ == "__main__":
    unittest.main()